//
//  SEGForgetPasswordViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/12.
//

#import "SEGForgetPasswordViewModel.h"
#import "SEGRequestManager.h"

@interface SEGForgetPasswordViewModel ()

@property (nonatomic, strong) SEGRequestManager *forgetManager;
@property (nonatomic, strong) SEGRequestManager *restPwdManager;

@end

@implementation SEGForgetPasswordViewModel

- (instancetype)initWithPhoneNumber:(NSString *)phoneNumer {
    self = [super init];
    if (self) {
        self.phoneStr = phoneNumer;
        [self seg_createRequestConfigInfoSignal];
        [self seg_createRefreshImgSignal];
        [self seg_createCaptchasSignal];
        [self seg_createSoundCodeSignal];
        [self seg_createSureBtnSignal];
    }
    return self;
}
- (void)seg_createRequestConfigInfoSignal {
    @weakify(self);
    self.reqConfigSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        [self.forgetManager requestPasswordConfigInfo:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if ([result isKindOfClass:[NSString class]]) {
                [subscriber sendNext:result];
                [subscriber sendCompleted];
            }
        }];
        return nil;
    }];
}
- (void)seg_createRefreshImgSignal {
    @weakify(self);
    self.refreshImgSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        self.checkImgStr = @"";
        [self.forgetManager requestSecurityRefreshCheckCodeWithFinished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                if ([self.forgetManager.seg_segtelcaptchasbyregistermodel.imgCode length] > 0) {
                    NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:self.forgetManager.seg_segtelcaptchasbyregistermodel.imgCode options:NSDataBase64DecodingIgnoreUnknownCharacters];
                    UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
                    [subscriber sendNext:decodedImage];
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];

        return nil;
    }];
}
- (void)seg_createCaptchasSignal {
    @weakify(self);
    self.captchasSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        NSError *error = [self seg_checkCondition];
        if (error) {
            [subscriber sendError:error];
            return nil;
        }
        [self.forgetManager requestGetCaptchasByTel:self.phoneStr imgCode:self.checkImgStr randomToken:self.forgetManager.seg_segtelcaptchasbyregistermodel.randomToken Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:@""];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
 
        return nil;
    }];
}
- (void)seg_createSoundCodeSignal {
    @weakify(self);
    self.soundCodeSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        NSError *error = [self seg_checkCondition];
        if (error) {
            [subscriber sendError:error];
            return nil;
        }
        [self.forgetManager requestGetSoundCodeByTel:self.phoneStr imgCode:self.checkImgStr randomToken:self.forgetManager.seg_segtelcaptchasbyregistermodel.randomToken Type:@"forgetPwd" Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:errorMessage];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}
- (void)seg_createSureBtnSignal {
    @weakify(self);
    self.sureBtnSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        if (self.phoneStr.length != 11) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入正确的手机号码"}];
            [subscriber sendError:error];
            return nil;
        }
        if (self.freshPwdStr.length < 8) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"密码至少为8个字符"}];
            [subscriber sendError:error];
            return nil;
        }
        [self.restPwdManager requestUserAppRestPwdByTel:self.phoneStr smsCode:self.smsCodeStr randomToken:self.forgetManager.seg_segtelcaptchasbyregistermodel.randomToken password:self.freshPwdStr Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:errorMessage];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
    
        return nil;
    }];
}

// 检测条件
- (NSError *)seg_checkCondition {
    if (self.phoneStr.length == 0) {
        return [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入手机号码"}];
    } else if (self.phoneStr.length < 11) {
        return [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入正确的手机号码"}];
    } else if (self.checkImgStr.length == 0) {
        return [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入图片验证码"}];;
    }
    return nil;
}

#pragma mark - Getter
- (SEGRequestManager *)forgetManager {
    if (!_forgetManager) _forgetManager = [[SEGRequestManager alloc] init];
    return _forgetManager;
}
- (SEGRequestManager *)restPwdManager {
    if (!_restPwdManager) _restPwdManager = [[SEGRequestManager alloc] init];
    return _restPwdManager;
}

@end
